package com.lizonghui.googleplay.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.bean.DownLoadInfo;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.control.DetailDesHolder;
import com.lizonghui.googleplay.control.DetailDownHolder;
import com.lizonghui.googleplay.control.DetailInfoHolder;
import com.lizonghui.googleplay.control.DetailPicHolder;
import com.lizonghui.googleplay.control.DetailSafeHolder;
import com.lizonghui.googleplay.manager.DownLoadManger;
import com.lizonghui.googleplay.protocol.DetailProtocol;
import com.lizonghui.googleplay.utils.UIUtils;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

public class DetailActivity extends AppCompatActivity {

    private String mPackageName;
   

    @Bind(R.id.detail_fl_des)
    FrameLayout mDetailDes;
    @Bind(R.id.detail_fl_info)
    FrameLayout mDetailInfo;
    @Bind(R.id.detail_fl_safe)
    FrameLayout mDetailSafe;
    @Bind(R.id.detail_fl_pic)
    FrameLayout mDetailPic;
    @Bind(R.id.detail_fl_down)
    FrameLayout mDetailDown;
    private DetailDownHolder mDetailDownHolder;
    private ItemInfoBean mItemInfoBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //逻辑顺序要理清，很重要
        initActionBar();//初始化工具栏
        init();//初始化，得到传递过来的数据
        initView();//初始化视图
    }

    private void initView() {
        LoadingPager loadingPager = new LoadingPager(UIUtils.getContext()) {
            @Override
            public LoadedResultState initData() {
                //这里就是DetailActivity具体加载数据
                return DetailActivity.this.initData();
            }

            @Override
            public View initSuccessView() {
                View mView = View.inflate(UIUtils.getContext(), R.layout.activity_detail, null);

                ButterKnife.bind(DetailActivity.this, mView);

                /*-----模块化视图设计------返回的是data+view，就是holder----------------*/
                /*-----应用信息部分---------这里布局都是用FrameLayout-------------*/
                DetailInfoHolder detailInfoHolder = new DetailInfoHolder();//得到一个iew，holder中的VIEW就是绑定了数据的
                mDetailInfo.addView(detailInfoHolder.mHolderView);//将holder中绑定了数据的view加入到info模块中
                detailInfoHolder.setDataAndRefreshHolderView(mItemInfoBean);//拿数据去刷新UI

                /*-----应用安全部分--------*/
                DetailSafeHolder detailSafeHolder = new DetailSafeHolder();
                mDetailSafe.addView(detailSafeHolder.mHolderView);
                detailSafeHolder.setDataAndRefreshHolderView(mItemInfoBean);

                /*-----应用截图部分--------*/
                DetailPicHolder detailPicHolder = new DetailPicHolder();
                mDetailPic.addView(detailPicHolder.mHolderView);
                detailPicHolder.setDataAndRefreshHolderView(mItemInfoBean);

                /*-----应用描述部分--------*/
                DetailDesHolder detailDesHolder = new DetailDesHolder();
                mDetailDes.addView(detailDesHolder.mHolderView);
                detailDesHolder.setDataAndRefreshHolderView(mItemInfoBean);

                /*-----应用下载部分--------*/
                mDetailDownHolder = new DetailDownHolder();

                /*==================添加观察者到观察者集合中==========*/
                DownLoadManger.getInstance().addObserver(mDetailDownHolder);
                /*==================添加观察者到观察者集合中==========*/

                mDetailDown.addView(mDetailDownHolder.mHolderView);
                mDetailDownHolder.setDataAndRefreshHolderView(mItemInfoBean);


                return mView;
            }
        };

        //activity都是通过setContentVIew告知具体展示的视图
        setContentView(loadingPager);//加载视图

        //触发加载数据
        loadingPager.triggerLoadData();
    }

    /**
     * @return
     * @des DetailActivity中真正加载数据
     */
    private LoadingPager.LoadedResultState initData() {
        //加载数据线程池，协议记载。这里实际上就是在线程中的了
        //这里都是根据后台给出的协议文档来发起请求
        DetailProtocol detailProtocol = new DetailProtocol(mPackageName);
        try {
            mItemInfoBean = detailProtocol.loadData(0);
            //这里不可以直接check，因为Fragment是继承BaseFragment中集成的check方法
            if (mItemInfoBean == null) {
                return LoadingPager.LoadedResultState.EMPTY;
            } else {
                return LoadingPager.LoadedResultState.SUCCESS;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }
    }

    private void init() {
        mPackageName = getIntent().getStringExtra("packageName");
    }


    //设置ActionBar,设置有回退键
    private void initActionBar() {
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("GooglePlay");
        //设置回退按钮
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }

    //设置回退操作逻辑
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /*-------------持有的观察者，需要和activity生命周期绑定---------------------*/

    @Override
    protected void onPause() {
        super.onPause();
        //移除观察者
        if (mDetailDownHolder != null) {
            DownLoadManger.getInstance().deleteObserver(mDetailDownHolder);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        //添加观察者
        if(mDetailDownHolder != null){
            DownLoadManger.getInstance().addObserver(mDetailDownHolder);

            //手动发布最新状态
            DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(mItemInfoBean);
            DownLoadManger.getInstance().notifyObservers(downLoadInfo);
        }
    }
    /*-------------持有的观察者，需要和activity生命周期绑定---------------------*/
    /**
     * 一定要熟悉流程，走程序流程很重要！！！！
     *
     * 安装-->移除观察者，因为跳到安装页面，detailActivity不可见了调用onPause
     * 完成-->添加了观察者
     * 1.现在的状态应该是已安装
     * 2，
     */
}
